package com.scau.yangsuiyu.estore.dababases.entity;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author suiyu_yang
 * @description
 * @date 2019-06-04 19:20
 * @email suiyu_yang@163.com
 */
@Entity
public class Shop {
    private int id;
    private String shopName;
    private String profile;
    private Timestamp createTime;
    private double qualityScore;
    private double serviceScore;
    private double deliveryScore;
    private String businessLicense;
    private Seller seller;
    private List<Goods> goodsList;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "seller_id")
    public Seller getSeller() {
        return seller;
    }

    public void setSeller(Seller seller) {
        this.seller = seller;
    }

    @Basic
    @Column(name = "shop_name")
    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    @Basic
    @Column(name = "profile")
    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    @Basic
    @Column(name = "create_time")
    public Timestamp getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    @Basic
    @Column(name = "quality_score")
    public double getQualityScore() {
        return qualityScore;
    }

    public void setQualityScore(double qualityScore) {
        this.qualityScore = qualityScore;
    }

    @Basic
    @Column(name = "service_score")
    public double getServiceScore() {
        return serviceScore;
    }

    public void setServiceScore(double serviceScore) {
        this.serviceScore = serviceScore;
    }

    @Basic
    @Column(name = "delivery_score")
    public double getDeliveryScore() {
        return deliveryScore;
    }

    public void setDeliveryScore(double deliveryScore) {
        this.deliveryScore = deliveryScore;
    }

    @Basic
    @Column(name = "business_license")
    public String getBusinessLicense() {
        return businessLicense;
    }

    public void setBusinessLicense(String businessLicense) {
        this.businessLicense = businessLicense;
    }

    @OneToMany(mappedBy = "shop", fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    public List<Goods> getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(List<Goods> goodsList) {
        this.goodsList = goodsList;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Shop shop = (Shop) o;
        return id == shop.id &&
                Double.compare(shop.qualityScore, qualityScore) == 0 &&
                Double.compare(shop.serviceScore, serviceScore) == 0 &&
                Double.compare(shop.deliveryScore, deliveryScore) == 0 &&
                Objects.equals(shopName, shop.shopName) &&
                Objects.equals(profile, shop.profile) &&
                Objects.equals(createTime, shop.createTime);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, shopName, profile, createTime, qualityScore, serviceScore, deliveryScore);
    }

    public void removeGoods(Goods goods) {
        Goods temp = null;
        for (Goods g : goodsList) {
            if (g.getId() == goods.getId()) {
                temp = g;
                break;
            }
        }
        if (temp != null)
            goodsList.remove(temp);
    }
}
